<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {

    /**
     *fiel:进入系统  根据不同的角色渲染不同的菜单权限
     */
   public function index(){

       $result = '';
       if(empty($_SESSION['type'])||empty($_SESSION['user_id'])||empty($_SESSION['appid'])){
           //session不存在 重定向到登录
           $this->redirect('Login/index');
           exit();
       }

       switch ($_SESSION['type']){
           case 'admin':
               $result = $this->check_session('admin_'.$_SESSION['user_id']);
               break;
           case 'teacher':
               $result = $this->check_session('teacher_'.$_SESSION['user_id']);
               break;
           case 'student':
               $result = $this->check_session('student_'.$_SESSION['user_id']);
       }
       if($result == false){
           $this->redirect('Login/index');
           exit();
       }

       //
       // $this->redirect('Login/index');
       //根据session的存储的登录类型和账号  去查询对应的权限表  渲染出权限数组
       $role      = $_SESSION['type'];
       $user_id   = $_SESSION['user_id'];
       //获取登录用户的信息
       $user_info = $this->userInfo($role,$user_id);
     //  print_r($user_info);

       $menu    = $this->user_action($role,$user_id);
       //渲染用户信息
       $this->assign('user_info',$user_info);
       ////渲染菜单
       $this->assign('list',$menu);
       $this->display();

   }

    /**
     * @param $str : session 字符缩影
     * @param $content   需要判断的内容
     */
   public function check_session($content){
       if($_SESSION['appid'] != $content){
           return false;
       }
       return true;

   }
   /**
    * 根据不同的角色查找不同的权限表   并且放入session中保存   用于后面接口掉用的权限检查
    */
   private function  user_action($role,$user_id){
       //初始化权限结果
       $result = "";
       switch ($role) {
           case 'admin':
               $admin_Obj = M('admin_action');
               $result = $admin_Obj->where('user_id=' . $user_id)->select();
               $_SESSION['action'] = $result;
               break;
           case 'teacher':
               $admin_Obj = M('teacher_action');
               $result = $admin_Obj->where('user_id=' . $user_id)->select();
               $_SESSION['action'] = $result;
               break;
           case 'student':
               $admin_Obj = M('student_action');
               $result = $admin_Obj->where('user_id=' . $user_id)->select();
               $_SESSION['action'] = $result;
               break;
            }
            //返回结果用于前端菜单渲染
       return $result;

       }
       private function  userInfo($type,$user_id){
               $user_Obj = M($type);
               $result   = $user_Obj->where('user_id='.$user_id)->find();
                return $result;

           }




}